(function (e) {
    "use strict";

    function LazyLoad(conf) {
        //全局触摸监听
        let doc = document,
            end = false,
            Agent = navigator.userAgent,
            el = doc.getElementById(conf.el);
        if (el !== null) {
            el.addEventListener("touchend", Load);
        }
        var isIE = Agent.indexOf("compatible") > -1 && Agent.indexOf("MSIE") > -1;
        if (isIE) {
            window.attachEvent("scroll", Load)
        } else {
            window.addEventListener("scroll", Load, false)
        }
        e.addEventListener("resize", Load, false)
        let p = conf.prop;
        var imgs = doc.querySelectorAll("img[" + p + "='true']"),
            i_len = 0,
            len = imgs.length,
            an = conf.attr || "_src",
            th = conf.th || 10,
            dataList = [],

            load = doc.createElement("style");
        load.innerHTML = ".loading{min-width:50px;min-height:50px}";
        for (var i = 0; i < len; i++) {
            dataList.push(imgs[i].getAttribute(an));
            imgs[i].classList.add("loading");
        }
        Load();
        doc.documentElement.appendChild(load);
        var id = null,
            delay = conf.delay || 150;

        function Load() {
            //重新获取
            i_len = imgs.length;
            if (!end) {
                if (i_len > 0) {
                    id = setTimeout(function () {
                        for (var i = 0; i < i_len; i++) {
                            let _tNode = imgs[i];
                            var top = getComputedStyle(_tNode, 't');
                            if ((top < window.innerHeight + th && (top > 0))) {
                                if (_tNode.src !== 'undefined' && eval(_tNode.getAttribute(p)) ===
                                    true) {
                                    _tNode.src = dataList[i];
                                    _tNode.removeAttribute(an);
                                    _tNode.classList.remove("loading");
                                    _tNode.removeAttribute(conf.prop);
                                }
                            }
                        }
                        clearTimeout(id)
                    }, delay);
                }
                if (doc.getElementsByClassName("loading").length <= 0) {
                    destory();
                }
            }

        }

        function getComputedStyle(node, value) {
            switch (value) {
                case 'l':
                    return node.getBoundingClientRect().left //|| imgs[i].offsetLeft;
                    break;
                case 't':
                    return node.getBoundingClientRect().top //|| imgs[i].offsetTop;
                    break;
            }
        }

        function destory() {
            // removeListener(el, "touchend");
            removeListener(e, "scroll");
        }

        function removeListener(v, b) {
            v.removeEventListener(b, Load);
            end = true;
        }
    };
    if (typeof exports !== 'object' && typeof module !== 'object') {
        e.__proto__.lazy = LazyLoad;
    }
})(window);
